package pers.warren.ai.oneapi.config;

import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import pers.warren.ai.oneapi.proxy.CacheMgmt;
import java.util.concurrent.TimeUnit;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

@Configuration(proxyBeanMethods = false)
public class CacheConfiguration {

    @Resource
    private OneApiConfiguration oneApiConfiguration;

    /**
     * 缓存管理
     */
    @Resource
    private CacheMgmt cacheMgmt;


    /**
     * 刷新缓存任务
     *
     * @author warren
     */
    @Bean
    public TimerTask refreshOneApiCacheTimeTask() {
        return new TimerTask() {
            @Override
            public void run() {
                cacheMgmt.refreshCache();
            }
        };
    }


    @Bean
    public ScheduledExecutorService scheduledThreadPool(TimerTask refreshOneApiCacheTimeTask) {
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(2);
        scheduledThreadPool.scheduleAtFixedRate(refreshOneApiCacheTimeTask, oneApiConfiguration.getRefreshTimeout(), oneApiConfiguration.getRefreshTimeout(), TimeUnit.MILLISECONDS);
        return scheduledThreadPool;
    }

}
